package org.wlw.obj;

/**
 * 在Java中所有的类都有一个父类，叫做Object
 */
public class TestObject {
    public static void main(String[] args) {
        Student stu = new Student();
        System.out.println(stu.getClass());
        //会得到这个对象内存的哈希码
        System.out.println(stu.hashCode());
        Student s1 = new Student();
        s1.no = "001";
        Student s2 = new Student();
        s2.no = "001";
        System.out.println(s1==s2);
        System.out.println(s1.equals(s2));
        System.out.println("-----------------------------------------------");
        //当直接调用sout输出对象的时候，会调用对象的toString方法.默认输出的是类名+@+哈希码的十六进制
//        System.out.println(s1.toString()+","+s1.hashCode());
        System.out.println(s1);
    }
}

//相当于 class Student extends Object
class Student extends Object{
    String no;

    @Override
    public String toString() {
        return "no:"+no;
    }

    //由于所有的类都继承于Object。所以如果参数是Object，意为着可以传递任意对象
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        Student s = (Student)o;
        return this.no.equals(s.no);
    }

    @Override
    public int hashCode() {
        return no != null ? no.hashCode() : 0;
    }
}